<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录">
      <!-- /导航栏 组件中登录成功有个关闭按钮点击关闭返回登录状态-->
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="user.mobile"
        :rules="userFormRules.mobile"
        maxlength="11"
        type="number"
      >
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field
        name="code"
        placeholder="请输入验证码"
        v-model="user.code"
        :rules="userFormRules.code"
        maxlength="6"
        type="number"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <!-- native-type="button"清除发送验证码按钮提交表单  -->
          <van-count-down
            :time="1000 * 10"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <!-- @finish="isCountDownShow = false"倒计时结束时触发 倒计时结束验证码按钮出现 -->
          <van-button
            v-else
            native-type="button"
            class="send-sms-btn"
            round
            size="small"
            type="default"
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>
<script>
import { login, getSmsCode } from "@/api/user.js";
export default {
  name: "LoginIndex",
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: "18682961204",
        code: "246810",
      },
      // 定义规则
      userFormRules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空",
          },
          {
            pattern: /^1[3|5|8|7|9]\d{9}$/,
            message: "手机格式错误",
          },
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空",
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误",
          },
        ],
      },
      isCountDownShow: false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit() {
      //提交请求登录
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });

      const user = this.user;
      //成功或者验证提示信息
      try {
        const res = await login(this.user);
        // 把token数据存储到vuex中
        this.$store.commit("setUser", res.data.data);
        console.log(res);
        this.$toast.success("登录成功");
        // 登录成功跳转原来页面 back不严谨
        this.$router.back()
      } catch (err) {
        // 一个参数(options)对象，包含要应用到
        //  response 上的任何自定义设置. 可能参数(options)是:
        // status: response 的状态码, 例如:200.400
        // statusText: 和状态码关联的状态消息, 例如: OK.
        if (err.response.status === 400) {
          console.log("登录失败");
          this.$toast.fail("手机号或者验证码格式错误");
        } else {
          console.log("登录失败");
          this.$toast.fail("登录失败");
        }
      }
    },
    //校验手机号  给发送验证码添加点击事件
    async onSendSms() {
      try {
        await this.$refs.loginForm.validate("mobile");
      } catch (err) {
        return console.log("登录失败", err);
      }
      //验证通过，倒计时显示出来
      this.isCountDownShow = true;
      //请求发送验证码
      try {
        await getSmsCode(this.user.mobile);
        this.$toast("发送成功");
      } catch (err) {
        //如果发送失败  倒计时不显示
        this.isCountDownShow = false;
        if (err.response.status === 429) {
          this.$toast("发送太频繁了,请稍后重试");
        } else {
          this.$toast("发送失败，请稍后重试");
        }
      }
      // this.isCountDownShow=false
    },
  },
};
</script>
<style scoped lang="less">
.login-container {
  .iconfont {
    font-size: 37px;
  }

  .send-sms-btn {
    // width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>